﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability.Domain;
using Traceability.ErrorCode;
using Traceability.Infrastructure;

namespace Traceability.App.Applications.DeepProcess
{
    public class VendorQueryCommandHandler : IRequestHandler<VendorQueryCommand, APIResult<T_Supplier>>
    {
        private readonly IBaseRepository<T_Supplier> supplierRepository;

        public VendorQueryCommandHandler(IBaseRepository<T_Supplier> supplierRepository)
        {
            this.supplierRepository = supplierRepository;
        }

        public Task<APIResult<T_Supplier>> Handle(VendorQueryCommand request, CancellationToken cancellationToken)
        {
            APIResult<T_Supplier> result = new APIResult<T_Supplier>();
            result.Code = APIEnum.ok;
            result.Message = "获取成功";
            result.Data = supplierRepository.GetAll().AsNoTracking().FirstOrDefault(x => x.Batch == request.Batch && x.Title.Contains(request.Title));

            return Task.FromResult(result);
        }
    }
}
